/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.support.v4.view;

import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutManager;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.app.AbilityContext;

import com.flipkart.ohos.proteus.DataContext;
import com.flipkart.ohos.proteus.ProteusConstants;
import com.flipkart.ohos.proteus.ProteusContext;
import com.flipkart.ohos.proteus.ProteusView;
import com.flipkart.ohos.proteus.ViewTypeParser;
import com.flipkart.ohos.proteus.managers.AdapterBasedViewManager;
import com.flipkart.ohos.proteus.processor.AttributeProcessor;
import com.flipkart.ohos.proteus.processor.BooleanAttributeProcessor;
import com.flipkart.ohos.proteus.support.v4.PageSliderLayoutmanager.PageSliderLayoutManagerFactory;
import com.flipkart.ohos.proteus.support.v4.adapter.PageSliderProviderFactory;
import com.flipkart.ohos.proteus.toolbox.Attributes;
import com.flipkart.ohos.proteus.value.AttributeResource;
import com.flipkart.ohos.proteus.value.Layout;
import com.flipkart.ohos.proteus.value.ObjectValue;
import com.flipkart.ohos.proteus.value.Resource;
import com.flipkart.ohos.proteus.value.StyleResource;
import com.flipkart.ohos.proteus.value.Value;

/**
 * PagerSliderParser
 *
 * @author adityasharat
 */
public class PagerSliderParser<V extends PageSlider> extends ViewTypeParser<V> {
    public static final String ATTRIBUTE_ADAPTER = "adapter";
    public static final String ATTRIBUTE_LAYOUT_MANAGER = "layout_manager";

    public static final String ATTRIBUTE_TYPE = ProteusConstants.TYPE;

    private final PageSliderProviderFactory pageSliderProviderFactory;

    private final PageSliderLayoutManagerFactory layoutManagerFactory;
    private AbilityContext context;

    public PagerSliderParser(PageSliderProviderFactory adapterFactory,
                             PageSliderLayoutManagerFactory layoutManagerFactory) {
        this.pageSliderProviderFactory = adapterFactory;
        this.layoutManagerFactory = layoutManagerFactory;
    }

    @Override
    public String getType() {
        return "PageSlider";
    }

    @Override
    public String getParentType() {
        return "ComponentContainer";
    }

    @Override
    public ProteusView createView(ProteusContext proteusContext, Layout layout,
                                  ObjectValue data, ComponentContainer parent, int dataIndex) {
        this.context = proteusContext.context;
        return new ProteusPageSlider(proteusContext);
    }

    @Override
    public ProteusView.Manager createViewManager(ProteusContext proteusContext, ProteusView view, Layout layout,
                                                 ObjectValue data, ViewTypeParser caller, ComponentContainer parent,
                                                 int dataIndex) {
        DataContext dataContext = createDataContext(proteusContext, layout, data, parent, dataIndex);
        return new AdapterBasedViewManager(proteusContext, null != caller ? caller : this, view.getAsView(), layout,
            dataContext);
    }

    @Override
    protected void addAttributeProcessors() {
        addAttributeProcessor(ATTRIBUTE_ADAPTER, new AttributeProcessor<V>() {
            @Override
            public void handleValue(V view, Value value) {
                if (value.isObject()) {
                    String type = value.getAsObject().getAsString(ATTRIBUTE_TYPE);
                    if (type != null) {
                        PageSliderProvider adapter = pageSliderProviderFactory.create(type, (ProteusPageSlider) view,
                            value.getAsObject(), context);
                        view.setProvider(adapter);
                    }
                }
            }

            @Override
            public void handleResource(V view, Resource resource) {
                throw new IllegalArgumentException("Recycler View 'adapter' expects only object values");
            }

            @Override
            public void handleAttributeResource(V view, AttributeResource attribute) {
                throw new IllegalArgumentException("Recycler View 'adapter' expects only object values");
            }

            @Override
            public void handleStyleResource(V view, StyleResource style) {
                throw new IllegalArgumentException("Recycler View 'adapter' expects only object values");
            }
        });

        addAttributeProcessor(ATTRIBUTE_LAYOUT_MANAGER, new AttributeProcessor<V>() {
            @Override
            public void handleValue(V view, Value value) {
                if (value.isObject()) {
                    String type = value.getAsObject().getAsString(ATTRIBUTE_TYPE);
                    if (type != null) {
                        LayoutManager layoutManager = layoutManagerFactory.create(type, (ProteusPageSlider) view,
                            value.getAsObject());
                        view.setLayoutManager(layoutManager);
                    }
                }
            }

            @Override
            public void handleResource(V view, Resource resource) {
                throw new IllegalArgumentException("Recycler View 'layout_manager' expects only object values");
            }

            @Override
            public void handleAttributeResource(V view, AttributeResource attribute) {
                throw new IllegalArgumentException("Recycler View 'layout_manager' expects only object values");
            }

            @Override
            public void handleStyleResource(V view, StyleResource style) {
                throw new IllegalArgumentException("Recycler View 'layout_manager' expects only object values");
            }
        });
        addAttributeProcessor(Attributes.ScrollView.ReboundEffect, new BooleanAttributeProcessor<V>() {
            @Override
            public void setBoolean(V view, boolean value) {
                view.setReboundEffect(value);
            }
        });
    }
}
